<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" title="登录"
    >
    <van-icon slot="left" name="cross" class="xx" @click="$router.back()" />
</van-nav-bar>
    <!-- 登录表单 -->
    <van-form ref="biaodanyanzheng" @submit="onSubmit">
      <van-field
        v-model="user.mobile"
        type="number"
        name="mobile"
        key=""
        placeholder="请输入手机号"
        :rules="userModel.mobile"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"> </i>
      </van-field>
      <van-field
        v-model="user.code"
        type="number"
        name="code"
        placeholder="请输入验证码"
        :rules="userModel.code"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>

        <template #button>
          <van-count-down v-if="isCountShow" :time="1000 * 10" format="ss s"
          @finish="isCountShow =false"
          />
          <van-button
            round
            class="send-sms-btn"
            size="small"
            type="primary"
            v-else
            @click="yanzheng"
            native-type="button"
            >发送验证码</van-button
          >
        </template>
      </van-field>

      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendSms } from '@/api/user.js'
export default {
  name: 'LoginPage',
  components: {},
  props: {},
  data () {
    return {
      user: {
        mobile: '',
        code: ''
      },
      // 表单验证，正则表达式
      userModel: {
        mobile: [
          {
            required: true,
            message: '手机号不能为空'
          },
          {
            pattern: /^1[3|5|8|9|7]\d{9}$/,
            message: '手机号格式错误'
          }
        ],
        code: [
          {
            required: true,
            message: '验证码不能为空'
          },
          {
            pattern: /^\d{6}$/,
            message: '验证码格式错误'
          }
        ]
      },
      isCountShow: false
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onSubmit () {
      const user = this.user
      try {
        // 轻提示toast组件 请求登录
        this.$toast.loading({
          message: '登录中...',
          forbidClick: true
        })
        const { data } = await login(user)
        this.$store.commit('setUser', data.data)
        this.$toast.success('登录成功')
        // 登录成功跳转回原来页面
        // back的方式不严谨
        this.$router.back()
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail('手机号或验证码输入错误')
        } else {
          this.$toast.fail('请求失败请稍后再试')
        }
      }
    },
    // 验证手机号
    async yanzheng () {
      try {
        console.log('验证通过')
        await this.$refs.biaodanyanzheng.validate('mobile')
      } catch (err) {
        return console.log('验证失败', err)
      }
      this.isCountShow = true
      // 验证码处理，发送验证码
      try {
        const res = await sendSms(this.user.mobile)
        console.log('发送成功', res)
      } catch (err) {
        console.log('发送失败', err)
      }
    }
  }
}
</script>
<style scoped lang="less">
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .send-sms-btn {
    width: 152px;
    height: 46px;
    background-color: #ededed;
    border: none;
    color: blue;
  }
}
</style>
